package com.niuke;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

/**
 * 60. 数字游戏
 * https://blog.csdn.net/qfc_128220/article/details/130775047
 * https://www.xiaohongshu.com/explore/64aea789000000001c00f0bd (解，利用前缀和)
 */
public class NiukeMoni60 {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        List<Integer> shouPai = new ArrayList<>(); // 手牌
        List<int[]> zhuoPai = new ArrayList<>(); // 桌牌的前缀和，不做桌牌的存储了，没啥用
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine(); // 切记一次循环只能调用一次 nextLine
            if ("end".equals(line)) {
                break;
            }
            if (shouPai.size() == zhuoPai.size()) {
                String[] line1 = line.split(" ");
                shouPai.add(Integer.parseInt(line1[1]));
            } else {
                String[] line2 = line.split(" ");
                int[] item = new int[line2.length];
                for (int i = 0; i < line2.length; i++) {
                    if (i == 0) {
                        item[i] = Integer.parseInt(line2[i]);
                    } else {
                        item[i] = Integer.parseInt(line2[i]) + item[i - 1];
                    }
                }
                zhuoPai.add(item);
            }
        }

        /**
         * nums[i, j] = zhuoPai.get(0).get(j) - zhuoPao.get(0).get(i)
         * 如果要 [zhuoPai.get(0).get(j) - zhuoPao.get(0).get(i)] % shouPai.get(0) == 0，
         * 则 zhuoPai.get(0).get(j) % shouPai.get(0) == zhuoPao.get(0).get(i) % shouPai.get(0)
         */

        List<Integer> result = new ArrayList<>();
        for (int i = 0; i < shouPai.size(); i++) {
            int currShouPai = shouPai.get(i);
            Set<Integer> quYu = new HashSet<>(); // 每个前缀和对手牌取余的记录
            int[] qianZuiHe = zhuoPai.get(i); // 前缀和数组

            boolean fuHe = false;
            for (int j = 0; j < qianZuiHe.length; j++) {
                int currQuYu = qianZuiHe[j] % currShouPai;
                if (currQuYu == 0 || quYu.contains(currQuYu)) {
                    fuHe = true;
                    break;
                }
                quYu.add(currQuYu);
            }

            if (fuHe) {
                result.add(1);
            } else {
                result.add(0);
            }
        }

        System.out.println(Arrays.toString(result.toArray()));

    }

}
